// ==UserScript==
// @name           Menu kontekstowe
// @namespace      http://code.google.com/p/plemiona-skrypty/source/browse/testowe/ContextMenu/
// @description    Skrypt dodaje menu kontekstowe do wiosek na mapce
// @include        http://pl*.plemiona.pl/game.php?*screen=map*
// ==/UserScript==

function ContextMenuWrapper() {

ContextMenu = new Class({
	initialize : function () {
		this.mapScrolled = false;
		this.log('initialize()', 4);
		this.addClickEvents();
	},
	onTableClick : function (ev) {
	},
	addClickEvents : function () {
		var cells = $('map').getElements('td'); 
		this.log('addClickEvents() :: cells to serve: '+cells.length);
		cells.each(function (cell) {
			if ($chk(cell.getElement('a'))) {
				//this.log('addClickEvents() :: cell#'+cell.get('id'));
				cell.addEvents({
					'mouseout' : function () { this.hideDelay = this.hideContextMenu.delay(500, this); }.bind(this),
					'mouseover' : function () { if ($chk(this.hideDelay)) {$clear(this.hideDelay);} }.bind(this),
					'mousedown' : this.showContextMenu.bindWithEvent(this),
					'contextmenu' : function (ev) { return false; }
				});
			}
		}.bind(this));
		$('ds_body').getElement('.map_container').getElements('td[onclick*="startMapScroll"]').addEvent('mousedown', function () {this.watchMapScrolls.delay(100,this);}.bind(this));
	},
	
	watchMapScrolls : function () {
		if ($chk(scrollMapId)) {
			//this.log('watchMapScrolls() :: scrollMapId: '+scrollMapId);
			this.watchMapScrolls.delay(100, this);
		} else {
			// scroll finished
			//this.log('watchMapScrolls() :: scroll finished');
			this.addClickEvents.delay(100, this);
		}
			
	},
	
	createContextDiv : function () {
		this.log('createContextDiv()');
		var _divContextMenu = new Element('div', {
			id : 'divContextMenu',
			html : '<list id="menu" style="list-style-type:none; z-index:104; padding:0px; white-space: nowrap"></list>',
			styles : {
				border : '1px solid #000',
				position : 'absolute',
				display : 'none',
				'z-index' : 103,
				'background-color' : '#F7EED3'
			},
			events : {
				mouseout : function () { this.hideDelay = this.hideContextMenu.delay(500, this);}.bind(this),
				mouseover : function () { if ($chk(this.hideDelay)) {$clear(this.hideDelay);} }.bind(this)
			}
		});
		$('ds_body').adopt(_divContextMenu);
		return _divContextMenu;
	},
	
	showContextMenu : function (ev) {
		this.log('showContextMenu()');
		this.hideContextMenu(ev);
		if (ev.event.button == 2) {
			var trg = $(ev.target);
			trg = trg.get('tag') == 'td' ? trg : trg.getParent('td');
			var isOwn = trg.getStyle('background-color');
			if (['#f0c800', '#ffffff'].contains(isOwn)) {
				isOwn = true;
			} else {
				isOwn = false;
			}
			this.log('showContextMenu() :: right click on: '+trg.get('tag')+'#'+trg.get('id') + '; has link: '+ $chk(trg.getElement('a')) + '; isOwn: ' + isOwn);
			var curVillageId = trg.get('html').match(/village=([0-9]+)/)[1];
			var villageId = trg.get('html').match(/id=([0-9]+)/)[1];
			var tid = trg.get('html').match(/t=[0-9]+/);
			if (!tid) {
				tid = '';
			} 
			var xyPattern = new RegExp( "[(]{1}[0-9]+[|]{1}[0-9]+[)]{1}" );
			var coords = xyPattern.exec(trg.getElement('a').onmouseover);
			coords = coords[0].substr( 1, coords[0].length - 2 );
			var x = coords.substr( 0, coords.indexOf( "|" ) );
			var y = coords.substr( coords.indexOf("|") + 1, coords.length - coords.indexOf("|") - 1 );
			
			var menu = this.createContextDiv();
			if (window.opera) {
				this.operaNativeContextPreventor(ev);
			}
			menu.getElement('list').adopt(
					new Element('li').setStyle('padding-left', '0px').adopt(new Element('a', {
						html : 'Info',
						href : "game.php?village="+curVillageId+"&screen=info_village&id="+villageId + tid
					})),
					new Element('li').setStyle('padding-left', '15px').adopt(new Element('a', {
						html : 'Przegląd wioski',
						href : (isOwn ? "game.php?village="+villageId+"&screen=overview"+ tid : '#'),
						styles : { color : (isOwn ? '' : '#c7bea3') }
					})),
					new Element('li').setStyle('padding-left', '15px').adopt(new Element('a', {
						html : 'Rekrutacja',
						href : (isOwn ? "game.php?village="+villageId+"&screen=train"+ tid : '#'),
						styles : { color : (isOwn ? '' : '#c7bea3') }
					})),
					new Element('li').setStyle('padding-left', '15px').adopt(new Element('a', {
						html : 'Pałac',
						href : (isOwn ? "game.php?village="+villageId+"&screen=snob"+ tid : '#'),
						styles : { color : (isOwn ? '' : '#c7bea3') }
					})),
					new Element('li').setStyle('padding-left', '15px').adopt(new Element('a', {
						html : 'Plac',
						href : (isOwn ? "game.php?village="+villageId+"&screen=place"+ tid : '#'),
						styles : { color : (isOwn ? '' : '#c7bea3') }
					})),
					new Element('li').setStyle('padding-left', '15px').adopt(new Element('a', {
						html : 'Wyślij wojska',
						href : "game.php?screen=place&mode=command&target="+villageId+"&village="+curVillageId + tid
					})),
					new Element('li').setStyle('padding-left', '15px').adopt(new Element('a', {
						html : 'Wyślij surowce',
						href : "game.php?&screen=market&mode=send&target="+villageId+"&village="+curVillageId + tid
					})),

					new Element('li').setStyle('padding-left', '30px').adopt(new Element('a', {
						html : 'z tej wioski',
						href : (isOwn ? "game.php?&screen=market&mode=send&target="+curVillageId+"&village="+villageId + tid : '#'),
						styles : { color : (isOwn ? '' : '#c7bea3') }
					})),
					new Element('li').setStyle('padding-left', '15px').adopt(new Element('a', {
						html : 'Grupy wioski',
						href : (isOwn ? "javascript:popup_scroll('groups.php?&mode=village&village_id="+villageId+tid+"', 300, 400);" : '#'),
						styles : { color : (isOwn ? '' : '#c7bea3') }
					})),
					new Element('li').setStyle('padding-left', '0px').adopt(new Element('a', {
						html : 'Scentruj mapę',
						href : "game.php?screen=map&x="+x+"&y="+ y + "&village=" + curVillageId + tid
					})),
					new Element('li').setStyle('padding-left', '30px').adopt(new Element('a', {
						html : 'i uaktywnij wioskę',
						href : (isOwn ? 'game.php?screen=map&village=' + villageId + tid : '#'),
						styles : { color : (isOwn ? '' : '#c7bea3') }
					}))
			);	
			menu.setStyles({
				display : 'block',
				top : (ev.page.y+5),
				left : (ev.page.x+5)
			});
			return false;
		}
	},
	hideContextMenu : function (ev) {
		this.log('hideContextMenu()');
		var _dMenu = $('divContextMenu');
		if ($chk(_dMenu)) {
			_dMenu.dispose();
		}
		return true;
	},

	operaNativeContextPreventor : function (ev) {
		this.log('operaNativeContextPreventor()');
		var div = new Element('div' , {
			id : 'OperaContextMenuPreventor',
			html : '<input type="button" style="width: 10px; height: 10px;" value=" " />',
			styles : {
				position : 'absolute',
				//top : (event.clientY + window.pageYOffset - 5) + 'px',
				//left : (event.clientX + window.pageXOffset - 5) + 'px',
				top : (ev.page.y-5)+'px',
				left : (ev.page.x-5)+'px',
				display : 'block',
				'z-index' : '500',
				width : '10px',
				height : '10px'
			}
		});
		$('ds_body').adopt(div);
		setTimeout('$("OperaContextMenuPreventor").dispose()', 300);
		//return false;
	},
	
	log : $empty
}); // Class::ContextMenu

var _contextMenu = new ContextMenu();

} // ContextMenuWrapper

if ((typeof GM_getResourceURL) != "undefined") {
	unsafeWindow.ContextMenu = {};
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.innerHTML = "(" + ContextMenuWrapper + ")();";
	script.charset = 'utf-8';
	document.body.appendChild(script);
} else {
	var ContextMenu;
	ContextMenuWrapper();
}



